package com.sds.replicator.helpers;


import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.logging.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: sdsbirdman
 * Date: 7/2/11
 * Time: 10:24 AM
 * To change this template use File | Settings | File Templates.
 */
@Deprecated
public class TransformationUtils {

    private static Logger LOGGER = Logger.getLogger(TransformationUtils.class.getName());

    private static TransformerFactory transformerFactory = TransformerFactory.newInstance();

    public static byte[] transformsByte(byte[] xsl, byte[] xml) {
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            Transformer transformer = transformerFactory.newTransformer(new StreamSource(toInputStream(xsl)));
//            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.transform(
                    new StreamSource(toInputStream(xml)),
                    new StreamResult(byteArrayOutputStream));
            return byteArrayOutputStream.toByteArray();
        } catch (TransformerConfigurationException e) {
            LOGGER.warning(e.getMessage());
        } catch (TransformerException e) {
            LOGGER.warning(e.getMessage());
        }
        return new byte[0];
    }

    private static InputStream toInputStream(byte[] bytes) {
        return new ByteArrayInputStream(bytes);
    }

}
